#####################################################################
# Platform dependent                                                #
#####################################################################
ifeq ($(PLATFORM),windows)
	export GCCDIR = /cygdrive/c/eyenix/or32-new-rc4
	BIN2HEX = bin2hex.exe
	DIS2HEX = dis2hex.exe
	MKDIR_P = mkdir.exe -p
	VERIFY =
	export CVER = 0rc4
else
	PLATFORM = linux
	export GCCDIR = /opt/or32-new-rc4
	BIN2HEX = bin2hex
	DIS2HEX = dis2hex
	MKDIR_P = mkdir -p
	VERIFY = #verify
	export CVER = 0rc4
endif

##build option###########################################################
export  CONFIG_ETH_SUPPORT = y
export  CONFIG_WIFI_SUPPORT = n
export  CONFIG_ONVIF_SUPPORT = n
export  CONFIG_ACTIVEX_SUPPORT = n
#export  WIFI_MODULE = sd8787
export  WIFI_MODULE = sd8782
#export  WIFI_MODULE = sd8777
#export  WIFI_MODULE = TICC3100
#########################################################################

TARGET = EN673

export SRC_BOOT = boot
export SRC_CPU0 = cpu0
export SRC_CPU1 = cpu1

ifeq ($(MODE),debug)
	export EXTRA_CFLAGS := -D_DEBUG -D_BENCHMARK
else ifeq ($(MODE),trace)
	export EXTRA_CFLAGS := -D_DEBUG -D_BENCHMARK
	export STB_CFLAGS := -finstrument-functions -D_STB_ENABLE #-D_STB_NAME_ENABLE
	export INLINE_CFLAGS := -D_INLINE
else ifeq ($(MODE),sim)
	export EXTRA_CFLAGS := -D_SIM_MODE
else ifeq ($(MODE),sim_debug)
	export EXTRA_CFLAGS := -D_DEBUG -D_SIM_MODE
else
	MODE = release
	export INLINE_CFLAGS := -D_INLINE
endif


# Include System Settings FIle (.mk) / Generated by GUI Tool
export CONFIG_FILE = config_SettingScript.mk
#export CONFIG_FILE = ENX_EVM_AR0237_V1.0.mk
#export CONFIG_FILE = ENX_EVM_IMX291_V2.0.mk
#export CONFIG_FILE = ENX_EVM_IMX327_V2.1.mk
#export CONFIG_FILE = ENX_EVM_IMX307_V2.2.mk
#export CONFIG_FILE = ENX_EVM_IMX291_FixedLens_V2.0.mk
#export CONFIG_FILE = ENX_EVM_IP_Encoder_BT656.mk
#export CONFIG_FILE = ENX_EVM_IP_Encoder_BT1120.mk

# This flag prevents including <stdio.h>
#EXTRA_CFLAGS += -D_STDIO_H_

export PLATFORM MODE EXTRA_CFLAGS

export TOPDIR	= $(CURDIR)
export CPU0DIR	= $(TOPDIR)/$(SRC_CPU0)
export CPU1DIR	= $(TOPDIR)/$(SRC_CPU1)
export OUTDIR	= $(TOPDIR)/output/module
export OUTBIN	= $(TOPDIR)/output

export BUILDRPT	= $(OUTDIR)/build.rpt

#####################################################################
#  Make Target Description                                          #
# $Examples: default(*)                                             #
#       - for release(*):          make                             #
#       - for debug:               make MODE=debug                  #
#       - for trace:               make MODE=trace                  #
#       - for rtl sim.:            make MODE=sim                    #
#       - for rtl sim. & debug:    make MODE=sim_debug              #
#                                                                   #
#       - for debug @ linux(*):    make                  MODE=debug #
#       - for debug @ windows:     make PLATFORM=windows MODE=debug #
#                                                                   #
#       - ip for debug @ linux(*): make ip               MODE=debug #
#####################################################################
all: clean_build clean compile install $(VERIFY)

isp_lib: clean_build compile_isp_lib check_build

$(SRC_BOOT): clean_build clean_$(SRC_BOOT) compile_$(SRC_BOOT) install $(VERIFY)
#$(SRC_BOOT): clean_build compile_$(SRC_BOOT) install $(VERIFY)

$(SRC_CPU0): clean_build clean_$(SRC_CPU0) compile_$(SRC_CPU0) install $(VERIFY)
#$(SRC_CPU0): clean_build compile_$(SRC_CPU0) install $(VERIFY)

$(SRC_CPU1): clean_build clean_$(SRC_CPU1) compile_$(SRC_CPU1) install $(VERIFY)
#$(SRC_CPU1): clean_build compile_$(SRC_CPU1) install $(VERIFY)

##############################
#  Clean Target Description  #
##############################
clean: clean_build clean_$(SRC_BOOT) clean_$(SRC_CPU0) clean_$(SRC_CPU1)
	@rm -rf $(OUTBIN)/$(TARGET).*

clean_build:
	@rm -f $(BUILDRPT)

clean_$(SRC_BOOT):
	@$(MAKE) clean -s -C $(SRC_BOOT)
	@rm -rf $(OUTDIR)/*$(SRC_BOOT)*.*

clean_$(SRC_CPU0):
	@$(MAKE) clean -s -C $(SRC_CPU0)
	@rm -rf $(OUTDIR)/*$(SRC_CPU0)*.*

clean_$(SRC_CPU1):
	@$(MAKE) clean -s -C $(SRC_CPU1)
	@rm -rf $(OUTDIR)/*$(SRC_CPU1)*.*

##############################
#  Make Description          #
##############################
#compile: create_outdir compile_$(SRC_BOOT) compile_$(SRC_CPU0) compile_$(SRC_CPU1)
compile: compile_$(SRC_BOOT) compile_$(SRC_CPU0) compile_$(SRC_CPU1)

compile_$(SRC_BOOT):
	@echo ""                                                                    2>&1|tee -a $(BUILDRPT)
	@echo "*******************************************************************" 2>&1|tee -a $(BUILDRPT)
	@echo "Compile $(SRC_BOOT)..."                                              2>&1|tee -a $(BUILDRPT)
	@$(MAKE) -s -C $(SRC_BOOT)                                                  2>&1|tee -a $(BUILDRPT)

compile_isp_lib:
	@echo ""                                                                    |& tee -a $(BUILDRPT)
	@echo "*******************************************************************" |& tee -a $(BUILDRPT)
	@echo "Compile ISP Library..."                                              |& tee -a $(BUILDRPT)
	@$(MAKE) -s -C $(SRC_CPU0)/isp/isp_lib                                      |& tee -a $(BUILDRPT)

compile_$(SRC_CPU0):
	@echo ""                                                                    2>&1|tee -a $(BUILDRPT)
	@echo "*******************************************************************" 2>&1|tee -a $(BUILDRPT)
	@echo "Compile $(SRC_CPU0)..."                                              2>&1|tee -a $(BUILDRPT)
	@$(MAKE) -s -C $(SRC_CPU0)                                                  2>&1|tee -a $(BUILDRPT)

compile_$(SRC_CPU1):
	@echo ""                                                                    |& tee -a $(BUILDRPT)
	@echo "*******************************************************************" |& tee -a $(BUILDRPT)
	@echo "Compile $(SRC_CPU1)..."                                              |& tee -a $(BUILDRPT)
	@$(MAKE) -s -C $(SRC_CPU1)                                                  |& tee -a $(BUILDRPT)

##############################
#  Make Output Binary File   #
##############################
install: check_build
	@echo ""
	@echo "*******************************************************************"
	@echo "Generate output files..."
	@rm -rf $(OUTBIN)/*.*
	@cat $(OUTDIR)/$(SRC_BOOT).bin $(OUTDIR)/$(SRC_CPU0).bin $(OUTDIR)/$(SRC_CPU1).bin > $(OUTBIN)/$(TARGET).bin
#	@util/$(BIN2HEX) $(OUTBIN)/$(TARGET).bin 1 -size_word  > $(OUTBIN)/$(TARGET).hex

	@echo "$(SRC_BOOT).bin: `cat $(OUTDIR)/$(SRC_BOOT).bin | wc -c` bytes"
	@echo "$(SRC_CPU0).bin: `cat $(OUTDIR)/$(SRC_CPU0).bin | wc -c` bytes"
	@echo "$(SRC_CPU1).bin: `cat $(OUTDIR)/$(SRC_CPU1).bin | wc -c` bytes"
	@echo "Generate $(TARGET).bin: `cat $(OUTBIN)/$(TARGET).bin | wc -c` bytes"
	@echo " @PLATFORM: $(PLATFORM)"
	@echo " @MODE    : $(MODE)"
	@echo " @FLAGS   : $(EXTRA_CFLAGS) $(STB_CFLAGS) $(INLINE_CFLAGS)"
	@echo " "

check_build:
	@echo "`grep --count "warning:" $(BUILDRPT)` Warning(s), `grep --count "error:" $(BUILDRPT)` Error(s)"
	@echo " "

create_outdir:
	@$(MKDIR_P) $(OUTBIN)
	@$(MKDIR_P) $(OUTDIR)

verify:
	@util/$(DIS2HEX) $(OUTDIR)/$(SRC_BOOT)_vf.dis > $(OUTDIR)/$(SRC_BOOT)_vf.hex
	@util/$(DIS2HEX) $(OUTDIR)/$(SRC_CPU0)_vf.dis > $(OUTDIR)/$(SRC_CPU0)_vf.hex
	@cat $(OUTDIR)/$(SRC_BOOT)_vf.hex >> $(OUTDIR)/$(SRC_CPU0)_vf.hex
	@util/$(DIS2HEX) $(OUTDIR)/$(SRC_CPU1)_vf.dis > $(OUTDIR)/$(SRC_CPU1)_vf.hex
	@cat $(OUTDIR)/$(SRC_BOOT)_vf.hex >> $(OUTDIR)/$(SRC_CPU1)_vf.hex

	@echo "Generate hex files for verifying RTL simulation..."
	@echo ""

##############################
#  Make Zip                  #
##############################
ZIP_DATE = $(shell date '+%y%m%d%H%M%S')
ZIP_NAME = $(shell basename $(CURDIR))_$(ZIP_DATE).zip
#ZIP_NAME = $(shell basename $(CURDIR))_$(ZIP_DATE).tar.gz
zip: clean
	@find . -name "*.bak" -delete
	@find . -name "*.o" -delete
	@find . -name "*.nm" -delete
	@zip -r ../$(ZIP_NAME) boot cpu0 cpu1 dev lib output history.txt Makefile mk_files
#	@zip -r ../$(ZIP_NAME) ./
#	@tar -cvzf ../$(ZIP_NAME) ./
	@echo "Create $(ZIP_NAME) OK! `cat ../$(ZIP_NAME) | wc -c` bytes"
